//
//  settingViewController.m
//  tangDynasty
//
//  Created by 洪闽程 on 2018/11/15.
//  Copyright © 2018年 洪闽程. All rights reserved.
//
#define kWeakSelf               @weakify(self);
#define kStrongSelf             @strongify(self);
#define weakify(object)         autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#define strongify(object)       try{} @finally{} __typeof__(object) object = weak##_##object;

#import "settingViewController.h"
#import "helpViewController.h"
#import "aboutViewController.h"
#import "WipeCachePartition.h"
@interface settingViewController ()
@property (strong, nonatomic) WipeCachePartition *wipeCachePartition;//清除缓存
@property (weak, nonatomic) IBOutlet UILabel *cacheNumLab;

@end

@implementation settingViewController

-(void)viewDidAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.contentInset=UIEdgeInsetsMake(-20, 0, 0, 0);
    float cacheSize = [self.wipeCachePartition filePath];
    
    self.cacheNumLab.text = [NSString stringWithFormat:@"%.2f M",cacheSize];
    
    self.navigationItem.title=@"设置";
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        if (indexPath.row==2)
        {
            [self clearAction];
        }
    }
}


- (void)clearAction
{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要清除缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    @weakify(self);
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                 {
                                     @strongify(self);
                                     //清除缓存
                                     [self.wipeCachePartition clearFile];
                                     
                                     [self.wipeCachePartition clearCachSuccess:^{
                                         //
                                            float cacheSize = [self.wipeCachePartition filePath];
                                         
                                            self.cacheNumLab.text = [NSString stringWithFormat:@"%.2f M",cacheSize];
                                         
                                     }];
                                     
                                 }];
    [alertController addAction:cancleAction];
    [alertController addAction:sureAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (IBAction)shareClick:(id)sender
{
    
    UIImage *image=[UIImage imageNamed:@"删除"];
    
    UIActivityViewController *activityController=[[UIActivityViewController alloc]initWithActivityItems:@[image] applicationActivities:nil];
    [self.navigationController presentViewController:activityController animated:YES completion:nil];
  
}
- (IBAction)helpClick:(id)sender
{
    [self.navigationController pushViewController:[[helpViewController alloc]init] animated:YES];
}
- (IBAction)aboutClick:(id)sender
{
    [self.navigationController pushViewController:[[aboutViewController alloc]init] animated:YES];
}
- (IBAction)gotoAppStore:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1018221712"]];
}

@end
